package com.heima.common.exception;

import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 作用：拦截到controller层异常（持久层或者service层向上抛出），将异常统一进行友好返回
 *
 * @author: itheima
 * @create: 2022-01-14 08:57
 */

@Slf4j
@RestControllerAdvice  //等于@ControllerAdvice+@ResponseBody
public class BaseExceptionHandler {


    @ExceptionHandler(RuntimeException.class)
    public ResponseResult handlerRuntimeException(RuntimeException e) {
        log.error("全局运行时异常捕获，{}", e.getMessage());
        return ResponseResult.errorResult(500, e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult handlerException(Exception e) {
        log.error("全局异常捕获，{}", e.getMessage());
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

}
